package hsintiao.com.xintiaoAPP.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import hsintiao.com.xintiaoAPP.entity.PayBill;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class PoiUtil {

    public static void creatExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
                                        String directory) throws IOException {
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setHeaderHeight((double)100);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        if (workbook != null);{
            //将Excel文件放到本地路径下
            FileOutputStream os = new FileOutputStream(directory+fileName);
            workbook.write(os);
            os.close();
        }
    }

    public static void creatExcelMoreSheet(List<?> my,List<?> alipay,String title1,String title2,
                                           String sheet1,String sheet2,String fileName,String filePath,
                                           ExcelType type) throws IOException {
        ExportParams exportParams = new ExportParams(title1, sheet1);
        ExportParams exportParams2 = new ExportParams(title2, sheet2);
        exportParams.setHeaderHeight((double)100);
        Workbook workbook;
        if(ExcelType.HSSF.equals(type)){
            workbook=new HSSFWorkbook();
        }else {
            workbook=new XSSFWorkbook();
        }
        ExcelExportService service = new ExcelExportService();
        service.createSheet(workbook,exportParams, PayBill.class,my);
        service.createSheet(workbook,exportParams2, PayBill.class,alipay);
        if (workbook != null);{
            //将Excel文件放到本地路径下
            FileOutputStream os = new FileOutputStream(filePath+fileName);
            workbook.write(os);
            os.close();
        }
    }
}